<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/2 0002
 * Time: 9:22
 */

namespace App\Base\Middleware;

use App\Base\Exceptions\ApiException;
use App\Content\Models\WxUsersModel;
use Illuminate\Http\Request;
use Closure;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

/**
 * 普通会员权限
 * Class ApiPermissionMiddleware
 * @package App\Base\Middleware
 */
class ApiPermissionMiddleware
{



    /**
    * @return WxUsersModel
     */
   public function WxUsersModel(){
        return app()->make(WxUsersModel::class);
   }
    /**
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $token=$request->input("token");
        if(empty($token)){
            throw  new ApiException(ApiException::ERROR_AUTHORIZATION);
        }
        $string=base64_decode($token);
        $arr=json_decode($string,true);
        $key=$arr["token"];
        $mdKey=Cache::get($arr['uid']);
        if($key!=$mdKey){
            throw  new ApiException(ApiException::ERROR_AUTHORIZATION);
        }
        define("AUID",$arr['uid']);
        $response = $next($request);
        return $response;
    }
}